// __PUBLIC__/js/page/admin/content/complaints
require(['../../common'], function() {
  require(['content.complaints'])
})

define('content.complaints', [
  'jquery',
  'page/grid'
], function($, Grid) {
  'use strict';
  var $table = $('.table')
  var mygrid = new Grid($table)
  mygrid.init()

  function getSelected() {
    var selected = mygrid.getSelected()
    if (!selected.count) {
      alert(lang.pleaseSelectAData)
      return
    }
    return selected
  }

  function getASelected(t) {
    var current = mygrid.getCurrent(t)
    var selected = {
      id: [current.id],
      index: [current.index]
    }
    console.log(selected)
    return selected
  }

  $(document)
    .on('click', '.A_screen', function(e) {
      e.preventDefault()
      deletecomplaints(1, getASelected(this))
    })
    .on('click', '.A_restore', function(e) {
      e.preventDefault()
      deletecomplaints(0, getASelected(this))
    })
    .on('click', '#J_screen', function(e) {
      e.preventDefault()
      var selected = getSelected()
      deletecomplaints(1, selected)
    })
    .on('click', '#J_restore', function(e) {
      e.preventDefault()
      var selected = getSelected()
      deletecomplaints(0, selected)
    })
    .on('click', '#J_del', function(e) {
      e.preventDefault()
      var selected = getSelected()
      deletecomplaints(2, selected)
    })
    .on('click', '#J_search', function(e) {
      e.preventDefault()
       sureSearchAttach()
    })
  //isdel 0- 删除 1-恢复 2-彻底删除

  function deletecomplaints(isdel, selected) {
    $.post(APP + '?app=admin&m=Complaints&a=doDelComplaints', {
      ids: selected.id.join(),
      isdel: isdel
    }).done(function(res) {
      if (res == '1') {
        alert(lang.delSuccess)
        mygrid.del(selected.index)
      } else {
        alert(lang.delFails)
      }
    })
  }

  function sureSearchAttach() {
    var uname = $('#complaints').val()
    var str_get ='&uname=' + uname 
    location.href = APP + '?app=admin&m=Complaints&a=complaints' + str_get
  }
})
